//
//  ChooseGiftView.m
//  GiftNote
//
//  Created by wsh on 15/8/19.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "DorpDownListView.h"
#import "INHeader.h"
#import "DownListCollectionViewCell.h"
#import "Screen.h"


@implementation DorpDownListView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]){
    
    }
    return self;
}




#pragma mark - 初始化默认显示的View
- (instancetype)initWithFrame:(CGRect)frame datasourse:(id)datasourse delegate:(id)delegate{
    
    if (self = [super initWithFrame:frame]){
        
        self.backgroundColor = [UIColor whiteColor];
        
        // 初始化时候赋值为-1 表示当前没有展开的View
        currentExtendSection = -1;
        
        self.DorpDownDelegate = delegate;
        self.DorpDownDataSourse = datasourse;
        
        //  对象 场合 个性 价格
        NSInteger sectionNum = 0;
        if ([self.DorpDownDataSourse respondsToSelector:@selector(numberofSection)]){
            
            sectionNum = [self.DorpDownDataSourse numberofSection];
        }
        
        if (sectionNum == 0){
            
            self = nil;
        }
        

        CGFloat sectionWidth = (1.0 * (frame.size.width) / sectionNum);
        
        // 循环创建button
        for (int i = 0; i < sectionNum; i++)
        {
            UIButton *sectionBtn = [[UIButton alloc]initWithFrame:CGRectMake(sectionWidth * i, 1, sectionWidth, frame.size.height - 2)];
            // tag : 1000+
            sectionBtn.tag = KSectionBtnTag + i;
            
            // 添加点击事件
            [sectionBtn addTarget:self action:@selector(sectionBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            
            NSString *sectionBtnTitle = @"--";
            
            if ([self.DorpDownDataSourse respondsToSelector:@selector(firstTitleInSection:index:)]){
                
                sectionBtnTitle = [self.DorpDownDataSourse firstTitleInSection:i index:[self.DorpDownDataSourse defaultShowSection:i]];
            }
            
            // 设置基本属性 标题以及标题颜色
            [sectionBtn setTitle:sectionBtnTitle forState:UIControlStateNormal];
            [sectionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            sectionBtn.titleLabel.font = [UIFont systemFontOfSize:13];
            
            [self addSubview:sectionBtn];
            
            
            
            // 点击可改变的小箭头
            UIImageView *downArrow = [[UIImageView alloc] initWithFrame:CGRectMake(sectionWidth * i + (sectionWidth - 16), (self.frame.size.height - 12) / 2, 12, 12)];
            downArrow.image = [UIImage imageNamed:@"iconfont-iconxiangxia"];
            
            // tag : 2000+
            downArrow.tag = KDownArrowTag + i;
            [self addSubview:downArrow];
            
            // 灰色的线条
            if (i < sectionNum && i != 0)
            {
                UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(sectionWidth * i, frame.size.height / 4, 1, frame.size.height - frame.size.height / 4 - frame.size.height / 4)];
                lineView.backgroundColor = [UIColor lightGrayColor];
                [self addSubview:lineView];
            }
        }
    }
    return self;
}




#pragma mark - 判断当前是否有展开的View
- (BOOL)isShow{
    
    if (currentExtendSection == -1){
        
        return NO;
    }
    return YES;
}



#pragma mark - 设置对应标题section的显示标题
- (void)setTitle:(NSString *)title inSection:(NSInteger)section{
    
    UIButton *btn = (UIButton *)[self viewWithTag:KSectionBtnTag + section];
    [btn setTitle:title forState:UIControlStateNormal];
}



#pragma mark - button点击事件
- (void)sectionBtnAction:(UIButton *)button{
    

    // 当前点击的button
    NSInteger section = button.tag - KSectionBtnTag;
    
    // 当前的小箭头
    UIImageView *currentArrow = (UIImageView *)[self viewWithTag:(KDownArrowTag + currentExtendSection)];
    
    // 点击button的时候 小箭头180°旋转
    [UIView animateWithDuration:0.3 animations:^{
        currentArrow.transform = CGAffineTransformRotate(currentArrow.transform, DEGREES_TO_RADIANS(180));
    }];
    
    if (currentExtendSection == section){
        
        // 隐藏View
        [self hideExtendChooseView];
    } else {
        
        currentExtendSection = section;
        // 找到当前的小箭头
        UIImageView *currentArrow = (UIImageView *)[self viewWithTag:(KDownArrowTag + currentExtendSection)];
        // 旋转
        [UIView animateWithDuration:0.3 animations:^{
            currentArrow.transform = CGAffineTransformRotate(currentArrow.transform, DEGREES_TO_RADIANS(180));
        }];
        
        // 将当前的View展示出来
        [self showListViewInsection:currentExtendSection chooseIndex:[self.DorpDownDataSourse defaultShowSection:currentExtendSection]];
        
    }
    
//    NSLog(@"%ld  --- %@",(long)section,button.titleLabel.text);

}



#pragma mark - 将展开的View收起来
- (void)hideExtendChooseView{
    
    if (currentExtendSection != -1){
        
        currentExtendSection = -1;
        // 通过改变frame以及动画
        CGRect frame = self.downCollectionView.frame;
        // 设置高度为0
        frame.size.height = 0;
        [UIView animateWithDuration:0.3 animations:^{
            self.alphaBgView.alpha = 1.0;
            self.downCollectionView.alpha = 1.0;
            
            self.alphaBgView.alpha = 0.2;
            self.downCollectionView.alpha = 0.2;
            
            self.downCollectionView.frame = frame;
        } completion:^(BOOL finished) {
            
            // 从父视图上移除
            [self.downCollectionView removeFromSuperview];
            [self.alphaBgView removeFromSuperview];
        }];
    }
}


#pragma mark - 展示View
- (void)showListViewInsection:(NSInteger)section chooseIndex:(NSInteger)index
{
    if (!self.downCollectionView){
        
        // 创建集合视图
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.downCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y + self.frame.size.height, SCREEN_WIDTH, 130) collectionViewLayout:flowLayout];
        
        self.downCollectionView.backgroundColor = [UIColor colorWithRed:220/256.0 green:240/256.0 blue:240/256.0 alpha:1];
        self.downCollectionView.delegate = self;
        self.downCollectionView.dataSource = self;
        
        // 展示View出现的透明背景
        self.alphaBgView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y + self.frame.size.height, self.frame.size.width, self.bigSuperView.frame.size.height - self.frame.origin.y - self.frame.size.height)];
        self.alphaBgView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5];
        
        // 给透明背景添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgTapAction:)];
        [self.alphaBgView addGestureRecognizer:tap];
    }
    
    
        // 修改CollectionView的frame
        CGRect frame = self.downCollectionView.frame;

        frame.size.height = 0;
        self.downCollectionView.frame = frame;
        [self.bigSuperView addSubview:self.alphaBgView];
        [self.bigSuperView addSubview:self.downCollectionView];
        
        frame.size.height = 130;
        [UIView animateWithDuration:0.3 animations:^{
            self.alphaBgView.alpha = 0.2;
            self.downCollectionView.alpha = 0.2;
            
            self.alphaBgView.alpha = 1.0;
            self.downCollectionView.alpha = 1.0;
            self.downCollectionView.frame = frame;
        }];
        [self.downCollectionView reloadData];
}



#pragma mark - View出现时候 点击透明背景 回收View 箭头旋转
- (void)bgTapAction:(UITapGestureRecognizer *)tap{
    
    NSLog(@"点击了透明背景");
    UIImageView *currentV = (UIImageView *)[self viewWithTag:KDownArrowTag + currentExtendSection];
    [UIView animateWithDuration:0.3 animations:^{
        currentV.transform = CGAffineTransformRotate(currentV.transform, DEGREES_TO_RADIANS(180));
    }];
    [self hideExtendChooseView];
}



#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(90, 30);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return [self.DorpDownDataSourse numberofRowsInSection:currentExtendSection];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *dorpcellIndentifier = @"dorpDownCell";
    [collectionView registerClass:[DownListCollectionViewCell class] forCellWithReuseIdentifier:dorpcellIndentifier];
    
    DownListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:dorpcellIndentifier forIndexPath:indexPath];
    
    cell.title.text = [self.DorpDownDataSourse titleInSection:currentExtendSection index:indexPath.item];
    
    return cell;
}


#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.DorpDownDelegate respondsToSelector:@selector(chooseAtSection:index:)]){
        
        NSString *chooseCellTitle = [self.DorpDownDataSourse titleInSection:currentExtendSection index:indexPath.item];
        

        if (indexPath.item == 0) {
            if (currentExtendSection == 0) {
                chooseCellTitle = @"对象";
            } else if (currentExtendSection == 1){
                chooseCellTitle = @"场合";
            } else if (currentExtendSection == 2){
                chooseCellTitle = @"个性";
            } else {
                chooseCellTitle = @"价格";
            }
        }
        
        NSIndexPath *indexPath1 = [NSIndexPath indexPathForItem:indexPath.item inSection:currentExtendSection];
//        NSLog(@"%ld,%ld",(long)indexPath1.section,(long)indexPath1.item);
        
        UIButton *currentSectionBtn = (UIButton *)[self viewWithTag:KSectionBtnTag + currentExtendSection];
        [currentSectionBtn setTitle:chooseCellTitle forState:UIControlStateNormal];
        [self.DorpDownDelegate chooseAtSection:currentExtendSection index:indexPath.item];
        [self hideExtendChooseView];
        
//        NSLog(@"%@ --- %ld === %ld",currentSectionBtn.titleLabel.text,(long)indexPath.section,(long)indexPath.item);

        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        NSNotification *notification = [NSNotification notificationWithName:@"检测button的名字" object:indexPath1 userInfo:nil];
        [center postNotification:notification];
    }
}
@end



















